package com.secondGroup.controller;


import com.secondGroup.entity.User;
import com.secondGroup.entity.VirtualBankCard;
import com.secondGroup.service.BankCardService;
import com.secondGroup.service.UserOperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
//@RestController
@Controller
@RequestMapping("/user-operate")
public class UserOperateController {

    @Autowired
    private BankCardService bankCardServiceImpl;
    @Autowired
    private UserOperateService userOperateServiceImpl;

    /**
     * 查询绑定的银行卡
     * @param
     * @return
     */
    @GetMapping("/queryBankCard")
    public String queryBankCard(Model model , HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        String account = user.getAccount();
        List<VirtualBankCard> list = bankCardServiceImpl.queryBankCard(account);
        model.addAttribute("list",list);
        return "withdrawal";
    }
    /**
     * 进行提现操作
     * @return
     */
    @PostMapping("/withdrawal")
    public String withdrawal(HttpSession session, BigDecimal eMoney,
                             String ePayPassword, String cId, Model model){
        User user = (User) session.getAttribute("userInfo");
        String account = user.getAccount();
        String string = userOperateServiceImpl.withdrawal(account,eMoney,ePayPassword,cId);
        model.addAttribute("url1", "/e/user-operate/queryBankCard?account="+account);
        model.addAttribute("url2", "/e/user/index");
        model.addAttribute("value", string);
        model.addAttribute("withdrawals", eMoney);
        if (string.equals("提现失败")||string.equals("密码输入不正确")||string.equals("提现金额不正确")){
            return "withdrawalError";
        }
        return "withdrawalSuccess";
    }

    /**
     * 充值时查询银行卡
     * @param
     * @param model
     * @return
     */
    @GetMapping("/checkBankCard")
    public String checkBankCard(HttpSession session,Model model){
        User user = (User) session.getAttribute("userInfo");
        String account = user.getAccount();
        List<VirtualBankCard> list = bankCardServiceImpl.queryBankCard(account);
        model.addAttribute("list",list);
        return "recharge";
    }
    /**
     * 充值操作
     * @param
     * @param eMoney
     * @param payPassword
     * @param cId
     * @return
     */
    @PostMapping("/recharge")
    public String recharge(HttpSession session,BigDecimal eMoney,
                           String payPassword, String cId,Model model){
        User user  = (User) session.getAttribute("userInfo");
        String account = user.getAccount();
        String str = userOperateServiceImpl.recharge(account,eMoney,payPassword,cId);
        model.addAttribute("url3","/e/user-operate/checkBankCard?account="+account);
        model.addAttribute("url4", "/e/user/index");
        model.addAttribute("texts", str);
        model.addAttribute("recharges", eMoney);
        if (str .equals("密码输入不正确")||str.equals("充值失败")||str.equals("充值金额不正确")){
            return "rechargeError";
        }
        return "rechargeSuccess";
    }
}
